package org.snail.web.config;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import java.util.Arrays;

/**
 * Created by WJ on 2017/12/8.
 */
@Configuration
@PropertySource("classpath:context-datasource.properties")
public class MongoElConfig {


    @Value("${mongodb.host}")
    private String mongoHost;
    @Value("${mongodb.port}")
    private int mongoPort;
    @Value("${mongodb.database}")
    private String mongoDB;
    @Value("${mongodb.username}")
    private String mongoUserName;
    @Value("${mongodb.password}")
    private String mongoPassword;


    @Bean(name = "mongoTemplate")
    public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, mongoConverter);
        return mongoTemplate;

    }

    /**
     * MongoDB 配置
     *
     * @return mongo连接工厂
     */
    @Bean(name = "mongoDbFactory")
    public MongoDbFactory mongoDbFactory() throws Exception {
        MongoCredential credential = MongoCredential.createCredential(mongoUserName, mongoDB,
                mongoPassword.toCharArray());
        ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort);
        MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
        SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoDB);
        return mongoDbFactory;
    }

    @Bean(name="mongoConverter")
    public MongoConverter mongoConverter(MongoDbFactory mongoDbFactory) {

        MappingMongoConverter mongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory),
                new MongoMappingContext());
        // 不插入_class
        mongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
        return mongoConverter;
    }

//    @Bean
//    @Primary
//    public MongoTemplate mongoTemplate() throws Exception {
//        return new MongoTemplate(this.mongoDbFactory(), this.mongoConverter(this.mongoDbFactory()));
//    }

}
